package com.itheima.service.impl;

import com.aliyun.oss.AliyunOSS;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.annotion.MyAnnotion;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/7 23:47
 * @Desc:
 */
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    private String file = null;
    @Autowired
    private AliyunOSS aliyunOSS;

    //登录
    @Override
    public boolean login(String username, String password) {
        if (username == null)
            return false;
        List<Emp> emps = empMapper.findEmpByUsername(username);
        if (emps.size() == 0 || !emps.get(0).getPassword().equals(password))
            return false;
        return true;
    }

    //添加员工
    @Override
    @MyAnnotion
    public boolean addEmp(Emp emp) {
        if (emp.getUsername() == null || emp.getName() == null || emp.getGender() == null)
            return false;
        List<Emp> emps = empMapper.findEmpByUsername(emp.getUsername());
        if (this.file != null)
            emp.setImage(this.file);
        if (emps.size() == 0) {
            empMapper.addEmp(emp);
            this.file = null;
            return true;
        }
        return false;
    }

    //删除员工
    @Override
    @MyAnnotion
    public void deleteEmp(List<Integer> ids) {
        empMapper.deleteEmp(ids);
    }

    //修改员工信息
    @Override
    @MyAnnotion
    public boolean updateEmp(Emp emp) {
        List<Emp> emps = empMapper.findEmpByUsername(emp.getUsername());
        if (this.file != null)
            emp.setImage(this.file);
        if (emps.size() == 0 || (emp.getId().equals(emps.get(0).getId()))) {
            empMapper.updateEmp(emp);
            this.file = null;
            return true;
        }
        return false;
    }

    //查询员工信息，支持动态查询
    @Override
    public PageBean empList(String name, Integer gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        if (name.length() == 0)
            name = null;
        PageHelper.startPage(page, pageSize);

        List<Emp> emps = empMapper.empList(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) emps;

        return new PageBean((int) p.getTotal(), p.getResult());
    }

    //根据id查询员工信息
    @Override
    public List<Emp> findEmpById(Integer id) {
        List<Emp> emps = empMapper.findEmpById(id);

        return emps;
    }

    //根据用户名查询员工信息
    @Override
    public List<Emp> findEmpByUsername(String username) {
        List<Emp> emps = empMapper.findEmpByUsername(username);

        return emps;
    }

    //文件上传
    @Override
    public String fileUpload(MultipartFile image) throws Exception {
        String url = aliyunOSS.fileLoad(image, null);

        if (StringUtils.hasLength(url)) {
            this.file = url;
            return url;
        }
        return null;
    }
}
